package com.example.administrator.szb.view;

import android.content.Context;
import android.graphics.Color;
import android.view.View;

import com.bigkoo.pickerview.OptionsPickerView;
import com.bigkoo.pickerview.view.WheelOptions;
import com.example.administrator.szb.R;
import com.example.administrator.szb.util.Contants;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * Created by stone on 2018/1/23.
 */

public class TimePickUtils2 {
    private Context context;
    OnTimeSelectListener listener;
    public TimePickUtils2(Context context) {
        this.context = context;
        initData();
        ShowPickerView();
    }

    public TimePickUtils2( Context context,OnTimeSelectListener listener) {
        this.listener = listener;
        this.context = context;
        initData();
        ShowPickerView();
    }

    public void setListener(OnTimeSelectListener listener) {
        this.listener = listener;
    }

    OptionsPickerView pvOptions;

    List<String> yearList = new ArrayList<>();
    List<List<String>> monthLists = new ArrayList<>();

    /**
     * 解析城市数据
     */
    private void initData() {//解析数据
        Calendar selectedDate = Calendar.getInstance();//系统当前时间
        int year = selectedDate.get(Calendar.YEAR);
        int month = selectedDate.get(Calendar.MONTH)+1;
        yearList.add("不限");
        List<String> monthList1 = new ArrayList<>();
        monthList1.add("");
        for (int j=1;j<13;j++){
            monthList1.add(""+j+"月");
        }
        monthLists.add(monthList1);
        for (int i=0;i<30;i++){
            yearList.add(""+(year+i)+"年");
            List<String> monthList2 = new ArrayList<>();
            for (int k=1;k<13;k++){
                if (i==0){
                    if (k>=month){
                        monthList2.add(""+k+"月");
                    }
                }else {
                    monthList2.add(""+k+"月");
                }

            }
            monthLists.add(monthList2);
        }

    }


    /**
     * 弹出选择地址框
     */
    private void ShowPickerView() {// 弹出选择器

        pvOptions = new OptionsPickerView.Builder(context, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                String year = yearList.get(options1);
                if (year!=null){
                    year = year.replace("年","");
                }
                String months = monthLists.get(options1).get(options2);
                if (months!=null){
                    months = months.replace("月","");
                }
                if (listener!=null){
                    if (year.contains("限")){
                        listener.onSelect(year);
                    }else {
                        listener.onSelect(year+ "-"+ Contants.getTwoString(months));
                    }


                }
            }
        })

                .setTitleText("")
                .setTitleBgColor(context.getResources().getColor(R.color.white))
                .setSubmitColor(context.getResources().getColor(R.color.toolbar_home))
                .setCancelColor(context.getResources().getColor(R.color.cancel))
                .setLineSpacingMultiplier(3)
                .setLabels("","","")
                .setDividerColor(Color.BLACK)
                .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
                .setContentTextSize(18)
                .build();
        pvOptions.setOnItemSelectListener1(new WheelOptions.OnItemClickListeners() {
            @Override
            public void onItemClick(int pos1, int pos2, int pos3) {
                if (pos1==0){
                    pvOptions.setSelectOptions(0,0);
                }
            }
        });
        pvOptions.setOnItemSelectListener2(new WheelOptions.OnItemClickListeners() {
            @Override
            public void onItemClick(int pos1, int pos2, int pos3) {
                if (pos1==0&&pos2!=0){
                    pvOptions.setSelectOptions(1,2);
                }
            }
        });
        pvOptions.setPicker(yearList, monthLists);//三级选择器
        pvOptions.show();
    }
    public void show(){
        pvOptions.show();
    }
    public interface OnTimeSelectListener{
        void onSelect(String time);
    }
}
